package data.platform.timescale.internal.monitor;

import io.micrometer.core.instrument.MeterRegistry;
import io.r2dbc.pool.ConnectionPool;
import io.r2dbc.spi.ConnectionFactory;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

@ConditionalOnBean(name = "timeScaleConfig")
@Service
@Slf4j
@RequiredArgsConstructor
public class PoolMonitor {

    final ConnectionFactory connectionFactory;

    final MeterRegistry dataSetMeterRegistry;

    @PostConstruct
    public void monitor() {
        ConnectionPool connectionPool = (ConnectionPool) connectionFactory;
        new PoolingMetricsBinder(connectionPool).bindTo(dataSetMeterRegistry);
    }

}
